package twoD.hofem;


/**
 * This Class forms linear combination of given basis functions <b>B</b> with
 * given coefficients <b>C</b>:<br>
 * c1*b1 + c2*b2 + ... + cn*bn
 * 
 * 
 * @author <font style="background-color: black; color: white;">&nbsp;:: Team 1
 *         ::&nbsp;</font><a href="mailto:luowei.de@googlemail.com"><font
 *         style="background-color: red; color: white;">&nbsp; Luo
 *         &nbsp;</font></a><a
 *         href="mailto:enes.siljak@ruhr-uni-bochum.de"><font
 *         style="background-color: green; color: white;">&nbsp; Siljak
 *         &nbsp;</font></a><a href="mailto:engr_asifkhan@yahoo.com"><font
 *         style="background-color: blue; color: white;">&nbsp; Khan
 *         &nbsp;</font></a>
 * 
 */

public class LinearCombinationRToR implements FunctionRToR {

	private FunctionBasisOnR basis;
	private double[] coefficients;

	/**
	 * Constructs LinearCombinationRToR of basis functions <b>b</b> with
	 * specified coefficients <b>c</b>
	 * 
	 * @param b
	 *            Basis functions
	 * @param c
	 *            Coefficients
	 */
	public LinearCombinationRToR(FunctionBasisOnR b, double[] c)
			throws IllegalArgumentException {
		if (b.getDimension() != c.length)
			throw new IllegalArgumentException(
					"Number of coefficients does not match with the basis dimension");

		basis = b;
		coefficients = c;
	}

	/**
	 * Constructs LinearCombinationRToR of basis functions <b>b</b>.
	 * 
	 * @param b
	 *            Basis functions
	 */
	public LinearCombinationRToR(FunctionBasisOnR b) {
		basis = b;
	}

	/**
	 * Sets the basis for linear combination.
	 * 
	 * @param b
	 *            Basis functions
	 */
	public void setBasis(FunctionBasisOnR b) {
		basis = b;
	}

	/**
	 * Sets the coefficients of linear combination. If number of coefficients
	 * does not match with the basis dimension throws an exception.
	 * 
	 * @param c
	 *            Coefficients
	 * @throws IllegalArgumentException
	 */
	public void setCoefficients(double[] c) throws IllegalArgumentException {
		if (basis.getDimension() != c.length)
			throw new IllegalArgumentException(
					"Number of coefficients does not match with the basis dimension");

		coefficients = c;
	}

	public double derivativeAt(double x) {
		double result = 0;

		for (int i = 0; i < basis.getDimension(); i++)
			result += coefficients[i] * basis.getBasisFunction(i).derivativeAt(x);

		return result;
	}

	public double[] getBreakpoints() {
		return basis.getBreakpoints();
	}

	public Interval getDomain() {
		return basis.getDomain();
	}

	public int getP() {
		return basis.getP();
	}

	public double valueAt(double x) {
		double result = 0;

		for (int i = 0; i < basis.getDimension(); i++)
			result += coefficients[i] * basis.getBasisFunction(i).valueAt(x);

		return result;
	}

}
